package com.hspedu.newData.dmsxl.位运算;
/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-28 
 * @Version: 1.0
 */

public class LC67二进制求和 {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int ca = 0;
        for (int i = a.length() - 1,j = b.length() - 1; i >= 0 || j >= 0 ; i--,j--) {
            int sum = ca;
            sum += i >= 0 ? a.charAt(i) - '0' : 0;
            sum += j >= 0 ? b.charAt(j) - '0' : 0;
            sb.append(sum%2);
            ca = sum / 2;
        }
        sb.append(ca == 1 ? ca : "");
        return sb.reverse().toString();
    }
}
